package org.pet.mapper;

import org.apache.ibatis.annotations.*;
import org.pet.entity.Pet;

import java.util.List;

@Mapper
public interface PetMapper {

    @Select("select * from Pet where id=#{id}")
    Pet getInfoById(Integer id);

    @Select("select * from Pet where name=#{category}")
    Pet getInfoByCategory(String category);

    @Select({
            "<script>",
            "select * from pet",
            "<where>",
            "<if test = 'category != null'>(name=#{category} or nick_name=#{category})</if>",
            "</where>",
            "</script>"})
    List<Pet> list(String category);

    @Insert("insert into pet " +
            "(name,nick_name,min_age,max_age,pic,gender,size,hair_color,hair_size," +
            "min_weight,max_weight,origin,capabilities,base_info,conservation,created_time,modify_time) " +
            "values " +
            "(#{name},#{nickName},#{minAge},#{maxAge},#{pic},#{gender},#{size},#{hairColor},#{hairSize}," +
            "#{minWeight},#{maxWeight},#{origin},#{capabilities},#{baseInfo},#{conservation},now(),now())")
    int insert(Pet pet);

    @Delete("delete from pet where id = #{id}")
    int delete(Integer id);

    @Update({
            "<script>",
            "update pet",
            "<set>",
            "<if test = 'name != null'>name=#{name},</if>",
            "<if test = 'nickName != null'>nick_name=#{nickName},</if>",
            "<if test = 'minAge != null'>min_age=#{minAge},</if>",
            "<if test = 'maxAge != null'>max_age=#{maxAge},</if>",
            "<if test = 'pic != null'>pic=#{pic},</if>",
            "<if test = 'gender != null'>gender=#{gender},</if>",
            "<if test = 'size != null'>size=#{size},</if>",
            "<if test = 'hairColor != null'>hair_color=#{hairColor},</if>",
            "<if test = 'hairSize != null'>hair_size=#{hairSize},</if>",
            "<if test = 'minWeight != null'>min_weight=#{minWeight},</if>",
            "<if test = 'maxWeight != null'>max_weight=#{maxWeight},</if>",
            "<if test = 'origin != null'>origin=#{origin},</if>",
            "<if test = 'capabilities != null'>capabilities=#{capabilities},</if>",
            "<if test = 'baseInfo != null'>base_info=#{baseInfo},</if>",
            "<if test = 'conservation != null'>conservation=#{conservation},</if>",
            "modify_time = now()",
            "</set>",
            "where id=#{id}",
            "</script>"})
    int update(Pet pet);
}
